﻿using Common;
using PLC.Domain.Dto;
using PLC.Domain.Entity;

namespace PLC.Domain.Models;

/// <summary>
/// plc读取的数据模型  同一台设备  一次读取  只会产生一条数据的存储
/// </summary>
public class PLCReadModel
{
    public DtoProductionData? DtoProductionData { get; set; }
    public List<DtoBarcodeData> BarcodeDatas { get; set; }=new List<DtoBarcodeData>();
    public DeviceStatus? DeviceStatus { get; set; }
    public AirtightData? AirtightData { get; set; } 
    public RepairAirtightData? RepairAirtightData { get; set; }
    public PressureData? PressureData { get; set; }
    public DeviceBeat? DeviceBeat { get; set; }
    /// <summary>
    /// 设备良品率
    /// </summary>
    public YieldRate? YieldRate { get; set; }
    /// <summary>
    /// 测试通讯的值
    /// </summary>
    public string TestConnection { get; set; }
    /// <summary>
    /// 从plc读取上来的触发值
    /// </summary>
    public string TriggerValue { get; set; }
    public List<CountModel> CountModel { get; set; }=new List<CountModel>();

    public void CalcYieldRate(string deviceName)
    {
        var success = CountModel.Where(p => p.IsOk).Sum(p=>p.Count);
        var fail = CountModel.Where(p => !p.IsOk).Sum(p => p.Count);
        var total = success + fail;
        this.YieldRate = new YieldRate()
        {
            DeviceName = deviceName,
            Rate =total==0?0m:(success*1.0/total).ToString().ToDecimal()
        };
    }
}

